package net.cgsoft.widget.service;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.nfc.Tag;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import net.cgsoft.widget.R;
import net.cgsoft.widget.https.DownCallBack;
import net.cgsoft.widget.https.okhttp.FileRequest;
import net.cgsoft.widget.utils.Util;

import java.io.File;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class DownService extends Service {

    private static final String TAG = "DownService";
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private String mTitle;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate()");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new DownBinder();
    }


    public class DownBinder extends Binder {
        public DownService getService() {
            return DownService.this;
        }
    }

    public void handleActionDown(String url, String title, final DownCallBack callBack) {
        mTitle = title;
        FileRequest fileRequest = new FileRequest(this);
        fileRequest.downApp(url, new DownCallBack() {
            @Override
            public void downStart() {
                callBack.downStart();
            }

            @Override
            public void downIng(long progress, String size) {

                if (mBuilder == null) {
                    callBack.downIng(progress, size);
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        mBuilder.setProgress(100, (int) progress, false);
                    } else {
                        mBuilder.setContentText("后台下载中..." + progress + "%");
                    }
                    mNotifyManager.notify(0, mBuilder.build());
                }
            }

            @Override
            public void downFinish(File apkFile) {
                if (mBuilder == null) {
                    callBack.downFinish(apkFile);
                } else {
                    mNotifyManager.cancel(0);
                }
                Util.installApk(DownService.this, apkFile.getAbsolutePath());
                stopSelf();
            }
        });
    }

    public void publishNotification() {
        mNotifyManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle(mTitle)
                .setContentText("后台下载中...")
                .setSmallIcon(R.drawable.ic_launcher);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy()");
    }
}
